/**
 *  Eclipse Public License 1.0
 */
package org.fireblade.easysms;

import java.io.BufferedWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.app.Service;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * 
 */
public class Util {

  private static final String LOGTAG = MainActivity.EASY_SMS + Util.class.getSimpleName();
  
  static final String EMPTY_STRING = "";
  static final String NEWLINE = "\r\n";

  static final Calendar CALENDAR = Calendar.getInstance(Locale.GERMANY);



  protected static String[] getNameAndAddressAndPersonByThread(Service service, String threadId) {
    Log.d(LOGTAG, "Get name, addres, person by thread id: " + threadId);
    String[] result = new String[3];
    //TODO
    Log.d(LOGTAG, "Get name, addres, person by thread id returns: " + result[0] + "/" + result[1] + "/"
      + result[2]);
    return result;
  }


  protected static String getNameByContactId(int contactId, Service service) {
    Log.d(LOGTAG, "Get name by contact id: " + contactId);
    String result = null;
    //TODO
    Log.d(LOGTAG, "Get name by contact id returns: " + result);
    return result;
  }


  protected static String[] getNameAndContactByNumber(String address, Service service) {
    Log.d(LOGTAG, "Get name by number: " + address);
    String[] result = null;
    //TODO
    return result;
  }
  

  /**
   * format the date as millis
   * @param date the date
   * @param context the context
   * @return the formatted date
   */
  static String format(Context context, long date) {
    return getDay(context, date) + ", " + getTime();
  }

  private static String getDay(Context context, long date) {
    CALENDAR.setTimeInMillis(System.currentTimeMillis());
    int dayNow = CALENDAR.get(Calendar.DAY_OF_YEAR);
    CALENDAR.setTimeInMillis(date);
    int dayGiven = CALENDAR.get(Calendar.DAY_OF_YEAR);
    if (dayNow == dayGiven) {
      return context.getResources().getString(R.string.str_today);
    } else if (dayNow == dayGiven + 1) {
      return context.getResources().getString(R.string.str_yesterday);
    } else if (dayNow == dayGiven + 2) {
      return context.getResources().getString(R.string.str_yesterday_1);
    } else {
      String day = "";
      int dayOfWeek = CALENDAR.get(Calendar.DAY_OF_WEEK);
      switch (dayOfWeek) {
        case Calendar.MONDAY:
          day += context.getResources().getString(R.string.str_monday) + ", ";
          break;
        case Calendar.TUESDAY:
          day += context.getResources().getString(R.string.str_tuesday) + ", ";
          break;
        case Calendar.WEDNESDAY:
          day += context.getResources().getString(R.string.str_wednesday) + ", ";
          break;
        case Calendar.THURSDAY:
          day += context.getResources().getString(R.string.str_thursday) + ", ";
          break;
        case Calendar.FRIDAY:
          day += context.getResources().getString(R.string.str_friday) + ", ";
          break;
        case Calendar.SATURDAY:
          day += context.getResources().getString(R.string.str_saturday) + ", ";
          break;
        case Calendar.SUNDAY:
          day += context.getResources().getString(R.string.str_sunday) + ", ";
          break;
      }
      return day += CALENDAR.get(Calendar.DAY_OF_MONTH) + "." + (CALENDAR.get(Calendar.MONTH) + 1) + ".";
    }
  }


  private static String getTime() {
    // date in calendar should be the date of format(..) set in getDay(..)
    int min = CALENDAR.get(Calendar.MINUTE);
    return CALENDAR.get(Calendar.HOUR_OF_DAY) + ":" + (min < 10 ? "0" + min : min);
  }


  /**
   * format the date as date
   * @param date the date
   * @return the formatted date
   */
  static String format(Context context, Date date) {
    return format(context, date.getTime());
  }

  static int MAX_SMS_CHARS = 918;

  /**
   * @param writer
   * @throws Exception
   */
  static void writeScript(Context context, BufferedWriter writer, boolean draft) throws Exception {
    writer.write(" <script language=\"javascript\" type=\"text/javascript\">");
    //TODO    
    writer.write(" </script>");
  }
}